package com.kitty.game.fight.artifact.passive;

import com.kitty.game.skill.model.RoleSkill;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.SkillConst;
import com.kitty.game.fight.model.BattleUnit;
import com.kitty.game.fight.util.FightRateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 物极必反
 * 血量较低时有几率触发，触发后移除所有障碍状态。
 * 若无辅助状态则随机触发一个辅助技能
 */
public class ExtremeArtifactEffect extends ArtifactEffect {

    @Override
    public short getEffectId() {
        return 0;
    }

    @Override
    public ArtifactType getType() {
        return ArtifactType.EXTREME;
    }

    @Override
    public Boolean trigger(Fight fight, FightObject member, Object... params) {
        if (!checkBaseCondition(fight, member)) {
            return false;
        }
        return SpringUtils.getArtifactService().getArtifactRate(getType().getName(), member.getRole());
    }

    @Override
    protected boolean checkBaseCondition(Fight fight, FightObject actor) {
        if (!actor.isPlayer()) {
            return Boolean.FALSE;
        }
        if (SpringUtils.getArtifactService().getSpecialSkillId(actor) != SkillConst.SKILL_WU_JI) {
            return Boolean.FALSE;
        }
        double currHp = actor.getCurrLife();
        double maxHp  = actor.getMaxLife();
        double hpRate = currHp / maxHp;
        if (hpRate > 0.3) {
            return Boolean.FALSE;
        }
        if (FightRateUtil.isHit(50, 100)) {
            return Boolean.FALSE;
        }
        // 清除所有的障碍状态
        actor.clearObstacleBuff();

        if (!actor.hasAssistBuff()) {
            // 如无辅助状态，随机触发一个辅助技能
            List<RoleSkill> assistSkills = new ArrayList<>(actor.getRole().getSkillBox().getAllAssistSkills());
            if (assistSkills.size() > 0) {
                Collections.shuffle(assistSkills);
                RoleSkill selected = assistSkills.get(0);
                BattleUnit battleUnit = new BattleUnit(fight, actor, actor, selected, selected.getLevel());
                battleUnit.triggerMagic();
            }
        }

        return Boolean.TRUE;
    }

}
